package 第7周_二分查找法.二分查找法的变种;

import 第7周_二分查找法.二分查找法.BinarySearch;

public class Solution {

    /**
     * 大于 target 的最小值索引
     * @param data
     * @param target
     * @param <E>
     * @return
     */
    public static <E extends Comparable<E>> int upper(E[] data, E target){
        int l = 0, r = data.length;

        //在data[l， r] 中寻找
        while (l < r){
            int mid = l + (r - l) / 2;

            if(data[mid].compareTo(target) <= 0){
                l = mid + 1;
            }
            else{
                r = mid;
            }
        }
        return l;
    }

    public static void main(String[] args) {
        Integer[] arr = {1,1,3,3,5,5};
        for (int i = 0; i <= 6; i++) {
            System.out.print(Solution.upper(arr, i) + " ");
        }
        System.out.println();
    }
}
